﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class TNCNBO : Csla.BusinessBase<TNCNBO>
    {
        #region Business Properties

        private int? _TNCN = null;
        public int? TNCN
        {
            get { return this._TNCN; }
            set { this._TNCN = value; PropertyHasChanged("TNCN"); }
        }

        private int? _HSID = Session.HSID;
        public int? HSID
        {
            get { return this._HSID; }
            set { this._HSID = value; PropertyHasChanged("HSID"); }
        }

        private int? _DTID = null;
        public int? DTID
        {
            get { return this._DTID; }
            set { this._DTID = value; PropertyHasChanged("DTID"); }
        }

        private double? _DIENT = 0;
        public double? DIENT
        {
            get { return this._DIENT; }
            set { this._DIENT = value; PropertyHasChanged("DIENT"); }
        }

        private double? _G1M2 = Session.G1M2;
        public double? G1M2
        {
            get { return this._G1M2; }
            set { this._G1M2 = value; PropertyHasChanged("G1M2"); }
        }

        private int? _TYLE = null;
        public int? TYLE
        {
            get { return this._TYLE; }
            set { this._TYLE = value; PropertyHasChanged("TYLE"); }
        }

        private double? _HSKH = 1;
        public double? HSKH
        {
            get { return this._HSKH; }
            set { this._HSKH = value; PropertyHasChanged("HSKH"); }
        }

        private double? _PNOP = 0;
        public double? PNOP
        {
            get { return this._PNOP; }
            set { this._PNOP = value; PropertyHasChanged("PNOP"); }
        }

        private bool? _TTHU = false;
        public bool? TTHU
        {
            get { return this._TTHU; }
            set { this._TTHU = value; PropertyHasChanged("TTHU"); }
        }

        private string _GHIC = null;
        public string GHIC
        {
            get { return this._GHIC; }
            set { this._GHIC = value; PropertyHasChanged("GHIC"); }
        }

        private string _ZDT_T = null;
        public string ZDT_T
        {
            get { return this._ZDT_T; }
            set { this._ZDT_T = value; PropertyHasChanged("ZDT_T"); }
        }

        private double? _ZDT_V = 0;
        public double? ZDT_V
        {
            get { return this._ZDT_V; }
            set { this._ZDT_V = value; PropertyHasChanged("ZDT_V"); }
        }

        private string _ZTYLE_T = null;
        public string ZTYLE_T
        {
            get { return this._ZTYLE_T; }
            set { this._ZTYLE_T = value; PropertyHasChanged("ZTYLE_T"); }
        }

        private double? _ZTYLE_V = 1;
        public double? ZTYLE_V
        {
            get { return this._ZTYLE_V; }
            set { this._ZTYLE_V = value; PropertyHasChanged("ZTYLE_V"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; PropertyHasChanged("STT0"); }
        }

        protected override object GetIdValue()
        {
            return _TNCN.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newTNCN = -1;
        private TNCNBO()
        {
            _TNCN = newTNCN--;
        }

        public TNCNBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public TNCNBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static TNCNBO NewRootTNCNBO()
        {
            return DataPortal.Create<TNCNBO>(new RootCriteria());
        }

        internal static TNCNBO NewChildTNCNBO()
        {
            return DataPortal.Create<TNCNBO>(new ChildCriteria());
        }

        public static TNCNBO GetRootTNCNBO(int _TNCN)
        {
            return DataPortal.Fetch<TNCNBO>(new RootCriteria(_TNCN));
        }

        public static TNCNBO GetRootTNCNBOByWhere(string where)
        {
            return DataPortal.Fetch<TNCNBO>(where);
        }

        internal static TNCNBO GetChildTNCNBO(SqlDataReader dataReader)
        {
            return new TNCNBO(dataReader);
        }

        public static void DeleteTNCNBO(int _TNCN)
        {
            DataPortal.Delete(new RootCriteria(_TNCN));
        }

        public static void DeleteTNCNBO(int _TNCN, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_TNCN, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int TNCN;
            public bool directDelete = false;
            public RootCriteria(int _TNCN)
            {
                this.TNCN = _TNCN;
                this.directDelete = false;
            }

            public RootCriteria(int _TNCN, bool directDelete)
            {
                this.TNCN = _TNCN;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int TNCN;
            public bool directDelete = false;
            public ChildCriteria(int _TNCN)
            {
                this.TNCN = _TNCN;
                this.directDelete = false;
            }

            public ChildCriteria(int _TNCN, bool directDelete)
            {
                this.TNCN = _TNCN;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TNCNBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[TNCN] = " + criteria.TNCN);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TNCNBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["TNCN"] is DBNull))
                this._TNCN = (int)dataReader["TNCN"];
            if (!(dataReader["HSID"] is DBNull))
                this._HSID = (int)dataReader["HSID"];
            if (!(dataReader["DTID"] is DBNull))
                this._DTID = (int)dataReader["DTID"];
            if (!(dataReader["DIENT"] is DBNull))
                this._DIENT = (double)dataReader["DIENT"];
            if (!(dataReader["G1M2"] is DBNull))
                this._G1M2 = (double)dataReader["G1M2"];
            if (!(dataReader["TYLE"] is DBNull))
                this._TYLE = (int)dataReader["TYLE"];
            if (!(dataReader["HSKH"] is DBNull))
                this._HSKH = (double)dataReader["HSKH"];
            if (!(dataReader["PNOP"] is DBNull))
                this._PNOP = (double)dataReader["PNOP"];
            if (!(dataReader["TTHU"] is DBNull))
                this._TTHU = (bool)dataReader["TTHU"];
            if (!(dataReader["GHIC"] is DBNull))
                this._GHIC = (string)dataReader["GHIC"];
            if (!(dataReader["ZDT_T"] is DBNull))
                this._ZDT_T = (string)dataReader["ZDT_T"];
            if (!(dataReader["ZDT_V"] is DBNull))
                this._ZDT_V = (double)dataReader["ZDT_V"];
            if (!(dataReader["ZTYLE_T"] is DBNull))
                this._ZTYLE_T = (string)dataReader["ZTYLE_T"];
            if (!(dataReader["ZTYLE_V"] is DBNull))
                this._ZTYLE_V = (double)dataReader["ZTYLE_V"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TNCNBO_InsertBO");
            db.AddInParameter(cmd, "@HSID", SqlDbType.Int, this._HSID);
            db.AddInParameter(cmd, "@DTID", SqlDbType.Int, this._DTID);
            db.AddInParameter(cmd, "@DIENT", SqlDbType.Float, this._DIENT);
            db.AddInParameter(cmd, "@G1M2", SqlDbType.Float, this._G1M2);
            db.AddInParameter(cmd, "@TYLE", SqlDbType.Int, this._TYLE);
            db.AddInParameter(cmd, "@HSKH", SqlDbType.Float, this._HSKH);
            db.AddInParameter(cmd, "@PNOP", SqlDbType.Float, this._PNOP);
            db.AddInParameter(cmd, "@TTHU", SqlDbType.Bit, this._TTHU);
            db.AddInParameter(cmd, "@GHIC", SqlDbType.NVarChar, this._GHIC);
            db.AddInParameter(cmd, "@ZDT_T", SqlDbType.NVarChar, this._ZDT_T);
            db.AddInParameter(cmd, "@ZDT_V", SqlDbType.Float, this._ZDT_V);
            db.AddInParameter(cmd, "@ZTYLE_T", SqlDbType.NVarChar, this._ZTYLE_T);
            db.AddInParameter(cmd, "@ZTYLE_V", SqlDbType.Float, this._ZTYLE_V);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewTNCN", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._TNCN = (int)cmd.Parameters["@NewTNCN"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TNCNBO_UpdateBO");
            db.AddInParameter(cmd, "@TNCN", SqlDbType.Int, this._TNCN);
            db.AddInParameter(cmd, "@HSID", SqlDbType.Int, this._HSID);
            db.AddInParameter(cmd, "@DTID", SqlDbType.Int, this._DTID);
            db.AddInParameter(cmd, "@DIENT", SqlDbType.Float, this._DIENT);
            db.AddInParameter(cmd, "@G1M2", SqlDbType.Float, this._G1M2);
            db.AddInParameter(cmd, "@TYLE", SqlDbType.Int, this._TYLE);
            db.AddInParameter(cmd, "@HSKH", SqlDbType.Float, this._HSKH);
            db.AddInParameter(cmd, "@PNOP", SqlDbType.Float, this._PNOP);
            db.AddInParameter(cmd, "@TTHU", SqlDbType.Bit, this._TTHU);
            db.AddInParameter(cmd, "@GHIC", SqlDbType.NVarChar, this._GHIC);
            db.AddInParameter(cmd, "@ZDT_T", SqlDbType.NVarChar, this._ZDT_T);
            db.AddInParameter(cmd, "@ZDT_V", SqlDbType.Float, this._ZDT_V);
            db.AddInParameter(cmd, "@ZTYLE_T", SqlDbType.NVarChar, this._ZTYLE_T);
            db.AddInParameter(cmd, "@ZTYLE_V", SqlDbType.Float, this._ZTYLE_V);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_TNCN.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TNCNBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@TNCN", SqlDbType.Int, criteria.TNCN);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_TNCN.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
